import socket, xml.sax, pygame
from xml_parser import game_loop_handler_client
import threading, cPickle

class loop_client(threading.Thread):
    def __init__(self, game):
        self.game = game
        self.game_logic = self.game.game_logic
        
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.bind(("", 1976))
        self.sock.settimeout(5)
        threading.Thread.__init__(self)
    
    def run(self):
        stop = False
        while not self.game_logic.loop_quit:
            try:
                data, addr = self.sock.recvfrom(4000)
                detail_list = cPickle.loads(data)
                for detail in detail_list:
                    pygame.event.post(pygame.event.Event(pygame.USEREVENT, {'details': detail}))
                if responses == len(self.game.players) -1:
                    break
            except:
                pass
                
            if self.game_logic.loop_quit == True:
                stop = True
        self.sock.close()
